package com.flame.user.message.service;

import com.flame.core.id.IdWorker;
import com.flame.core.utils.DateUtils;
import com.flame.user.message.controller.vo.UserMessageVo;
import com.flame.user.message.entity.UserMessage;
import com.flame.user.message.mapper.UserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * flame_user_message 用户消息表
 *
 * @author 吴欣童
 * @since 2023-02-14 20:28:48
 */
@Service
public class UserMessageService {

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private IdWorker idWorker;

    public List<UserMessage> pageUserMessage(UserMessageVo userMessageVo) {
        return userMessageMapper.page(userMessageVo);
    }

    public void addUserMessage(UserMessage userMessage) {
        userMessage.setMessageId(idWorker.nextId());
        userMessageMapper.insert(userMessage);
    }

    public void completeUserMessage(List<UserMessage> userMessageList) {
        for (UserMessage userMessage : userMessageList) {
            userMessage.setMessageStatus("1");
            userMessage.setCompleteTime(DateUtils.currentDateTime());
            userMessageMapper.updateById(userMessage);
        }
    }

    public void deleteUserMessage(UserMessage userMessage) {
        userMessageMapper.deleteById(userMessage.getUserId(), userMessage.getMessageId());
    }

    public Integer countUserMessageByStatus(UserMessage userMessage) {
        return userMessageMapper.countUserMessageByStatus(userMessage);
    }
}
